%% Author: vivimice
%% Created: Jan 9, 2012
%% Description: worker process for each connection
-module(worker).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([create/1, init/1]).

%%
%% API Functions
%%

%% create worker process to handle individual connection
create(Socket) ->
	WorkerPid = spawn(worker, init, [Socket]),
	gen_tcp:controlling_process(Socket, WorkerPid),
	{ok, WorkerPid}.

init(Socket) ->
	logger:trap_error(),
	
	% init context
	Context = context:start_link(),
	context:set_socket(Context, Socket),
	
	% init io
	tio_manager:init(Context),
	
	%% start the actual business
	start(Context),
	
	%% close
	tio_manager:close(Context),
	context:stop(Context),
	
	erlang:exit(normal).

%%
%% Local Functions
%%
start(Context) ->
	login_screen:show(Context),
	ok.
